import asyncio
import time
import aiohttp
import blog_spider  # 假设这是一个包含urls的模块

# 协程函数，用于异步地爬取网页内容
async def async_craw(url: str):
    print("craw url: ", url)
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as resp:
            result = await resp.text()  # 异步等待获取响应内容
            print(f"craw url:{url}, {len(result)}")

# 获取事件循环
loop = asyncio.get_event_loop()

# 创建多个协程任务
tasks = [loop.create_task(async_craw(url)) for url in blog_spider.urls]

start = time.time()  # 记录开始时间
# 并发执行所有协程任务，并等待完成
loop.run_until_complete(asyncio.wait(tasks))
end = time.time()  # 记录结束时间

# 打印执行时间
print("use time seconds: ", end - start)
